﻿package beautifulWorld2D.engine {
	
	import beautifulWorld2D.engine.Decor;
	import beautifulWorld2D.engine.Perso;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.filesystem.File;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.ui.Keyboard;
	import flash.utils.getTimer;
	
	
	public class controlePerso {
		
		private var perso:Perso;
		private var decor:Decor;
		
		private var pressKeyLeft:Boolean = false;
		private var pressKeyRight:Boolean = false;
		private var pressKeyUp:Boolean = false;
		private var pressKeySpace:Boolean 	= false;
		private var pressKeyFlag:Boolean 	= false;
		
		private var pointCollision:Number;
		private var pointCollisionTab:Array;
		private var fallCount:Number = 0;
		private var countFrame:Number = 0;
		private var countFrame2:Number = 0;
		
		private var son_pas:Sound;
		private var son_reception:Sound;
		private var son_receptionChannel:SoundChannel;
		private var son_pasChannel:SoundChannel;
		
		private var box:MovieClip;
		private var _enable:Boolean;
		private var _scrollEnable:Boolean = true;
		
		

		function controlePerso(obj_perso:Perso,obj_decor:Decor){
			
			perso 	= obj_perso;
			decor 	= obj_decor;
			box 	= perso.box;
			pointCollisionTab = new Array();
			
			// son pas
			son_pas = new Sound(new URLRequest(File.applicationDirectory.resolvePath("sons/pas.mp3").url));
			
			// son son_reception
			son_reception =  new Sound(new URLRequest(File.applicationDirectory.resolvePath("sons/reception.mp3").url));
			
			/////////////////////////////////////////////////////////////////
			/// Ecouteurs touhes claviers
			/////////////////////////////////////////////////////////////////

			_enable = true;
			perso.clip.stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			perso.clip.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			perso.clip.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			
			
			perso.startTime = getTimer();
			perso.startTime2 = getTimer();
			perso.startTime3 = getTimer();
			
			decor.animCenterScreen(perso.x, perso.y, false);
		}
		//// quand on presse une touche
		function onKeyDown(evt:KeyboardEvent):void {
			
			if ( !_enable) {
				return;
			}
			
			////////////////////////////////////////	
			///// Touche droite
			if(evt.keyCode == Keyboard.RIGHT && !pressKeyLeft && !pressKeyRight){
					//trace("key right");
				pressKeyRight = true;
				perso.isRight = true;
				if(!perso.isRun && !perso.isJump && !perso.isCollision && !perso.isFall && !perso.isReception){
					perso.isRun = true;
					son_pasChannel = son_pas.play(0, 9999);
					perso.startTime = getTimer();
				}
			}
			////////////////////////////////////////	
			///// Touche gauche
			if(evt.keyCode == Keyboard.LEFT && !pressKeyRight && !pressKeyLeft){
					//trace("key left");
				pressKeyLeft = true;
				perso.isRight = false;
				if(!perso.isRun && !perso.isJump && !perso.isCollision && !perso.isFall && !perso.isReception){
					perso.isRun = true;
					son_pasChannel = son_pas.play(0, 9999);
					perso.startTime = getTimer();
				}
			}
			////////////////////////////////////////	
			///// Touche Haut
			if(evt.keyCode == Keyboard.SPACE && !perso.isJump && !perso.isFall && !pressKeyUp){
				//trace("key JUMP");
				pressKeyUp = true;
				perso.isJump = true;
				perso.acceleration = 15; // on met l'acceleration de la chute à 15 au cas ou le perso atteris dans le vide
				perso.startTime = getTimer();
			}
			////////////////////////////////////////	
			///// Touche Espace
			/*if(evt.keyCode == Keyboard.SPACE && !pressKeySpace){
				//trace("key SPACE");
				//pressKeySpace = true;
				perso.lens = !perso.lens;
			}*/

			
			
		}

		//// quand on relache une touche
		function onKeyUp(evt:KeyboardEvent) {
			

			//trace("onKeyUp");
			
			////////////////////////////////////////	
			///// Touche droite
			if(evt.keyCode == Keyboard.RIGHT && pressKeyRight ){
				//trace("key up right");
				if(!perso.isJump){
					perso.setBoxPosX();
				}
				pressKeyRight = false;
				perso.isRun = false;
				perso.isCollision = false;
				son_pasChannel.stop();
			}
			////////////////////////////////////////	
			///// Touche gauche
			if(evt.keyCode == Keyboard.LEFT && pressKeyLeft){
				//trace("key up left");
				if(!perso.isJump){
					perso.setBoxPosX();
				}
				pressKeyLeft = false;
				perso.isRun = false;
				perso.isCollision = false;
				son_pasChannel.stop();
			}
			////////////////////////////////////////	
			///// Touche haut
			if(evt.keyCode == Keyboard.SPACE && pressKeyUp){
				//trace("key up haut");
				pressKeyUp = false;
			}
			
			
		}

		
		// ecouteur roulette souris
		function onMouseWheel(evt:MouseEvent) {
			 decor.setZoom(-evt.delta);
		}
		
		public function destroy()
		{
			perso.clip.stage.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			perso.clip.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			perso.clip.stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		public function listenerAction(evt:Event = null){
			
			
			//PressKey_Listener.onKeyDown();
			//ReleaseKey_Listener.onKeyUp();
			
			var posBox_x:Number = perso.xBox;
			var posBox_y:Number = perso.yBox;
			


		
			
			/// anim stand
			if(!perso.isRun && !perso.isJump && !perso.isFall && !perso.isReception){
				if(!perso.isStand){
					perso.isStand = true;
					if( _scrollEnable) decor.animCenterScreen(posBox_x,posBox_y);
				}
				perso.standTimer();
			}else{
				perso.isStand = false;
			}
			
			
			
			///////////////////
			// test sol
			if(!perso.isJump){
				var poyStart:Number = posBox_y;
				perso.fall();
				pointCollisionTab = decor.getPointCollision_Sol(posBox_x, posBox_y, poyStart, perso.isRun);
				if ( pointCollisionTab[1] == -1 &&  pointCollisionTab[0] == -1 ) {
					fallCount++;
					perso.setBoxPosY();
					if(fallCount > 15){
						perso.isRun = false;
						perso.isFall = true;
						perso.standTimer();
					}
					perso.startTime3 = getTimer();
				}else{
						if(pointCollisionTab[0] != -1){
							perso.xBox = int(pointCollisionTab[0]);
							perso.setBoxPosX();
						}
						if(pointCollisionTab[1] != -1){
							perso.yBox = int(pointCollisionTab[1]);
							perso.setBoxPosY();
						}
						perso.initAcceleration();
						if(fallCount > 15){
							perso.startTime = getTimer();
							perso.isReception = true;
							son_reception.play(0, 1);
						}
						fallCount = 0;
						perso.isFall = false;
						perso.startTime3 = -1;
				}
			}
			

		
			
			if(!perso.isStand){
				
				/// anim run
				if(perso.isRun && !perso.isJump && !perso.isFall && !perso.isReception){
					perso.runTimer();
					
					if(perso.isRight){
						pointCollision = decor.getPointCollision_X(posBox_x+box.width/2, posBox_y-box.height/2,1);
						if ( pointCollision == -1) {
							perso.setAnim();
						}else{

								perso.isRun = false;
								perso.xBox = pointCollision-box.width/2;
								perso.setBoxPosX();  /// le perso prend les coordonnées du box de collision

						}
					}else{
						
						pointCollision = decor.getPointCollision_X(posBox_x-box.width/2, posBox_y-box.height/2,-1);
						if ( pointCollision == -1 ) {
							perso.setAnim();
						}else{

								perso.isRun = false;
								perso.xBox = pointCollision+box.width/2;
								perso.setBoxPosX();  /// le perso prend les coordonnées du box de collision
						}
					}
		
				}
				
				
				/// anim jump
				if(perso.isJump && !perso.isFall && !perso.isReception){
					poyStart = posBox_y;
					var etat  = perso.jumpTimer();

					if(etat == 1 || etat == 2){// si perso en descente de saut, on test si collision avec un sol
						pointCollisionTab = decor.getPointCollision_Sol(posBox_x, posBox_y, poyStart, false);
						if ( pointCollisionTab[1] == -1 ) {
							perso.setAnim();
							if(etat == 2){
								perso.isJump = false;
								perso.setBoxPosY();
								perso.setBoxPosX();
								perso.initAcceleration2();
								//perso.isFall = true;
							}
						}else{
							perso.yBox = pointCollisionTab[1];
							perso.setBoxPosY();  /// le perso prend les coordonnées du box de collision
							perso.setBoxPosX();
							perso.isReception = true;
							perso.startTime = getTimer();
							perso.isJump = false;
							son_reception.play(0, 1);
						}
					}else{
						perso.setAnim();
					}
				}
				

			}
			
			// deplacement droite gauche pendant le saut ou la chute
			if (pressKeyLeft && ( perso.isJump || perso.isFall)) {
				pointCollision = decor.getPointCollision_X(posBox_x-box.width/2, posBox_y-box.height/2,-1);
				if ( pointCollision == -1 ) {
					perso.translateX();
				}else{
					perso.xBox = pointCollision+box.width/2;
				}
				perso.setBoxPosX();
				perso.startTime2 = getTimer();
			}else{
				if (!pressKeyRight) perso.startTime2 = -1;
			}
			if (pressKeyRight && ( perso.isJump || perso.isFall)) {
				pointCollision = decor.getPointCollision_X(posBox_x+box.width/2, posBox_y-box.height/2,1);
				if ( pointCollision == -1 ) {
					perso.translateX();
				}else{
					perso.xBox = pointCollision-box.width/2;
				}
				perso.setBoxPosX();
				perso.startTime2 = getTimer();
			}else{
				if (!pressKeyLeft) perso.startTime2 = -1;
			}
			
			
			
			/// anim jump reception
			if(perso.isReception){
				perso.receptionTimer();
			}
			

			// si zoom
			if(decor.isZooming){
				decor.animZoom(posBox_x,posBox_y);
			}
			// si rotation
			if(decor.isRotate){
				decor.animRotation(posBox_x,posBox_y);
			}
			
			// scroll ecran
			if (_scrollEnable) {
				


				if ( _scrollEnable) {
					if ( !perso.isJump && !perso.isReception) {
						decor.animCenterScreen(posBox_x, posBox_y);
					}else {
						decor.animCenterScreen(posBox_x, perso.y);
					}
				}

			}

			
			
			// checkDecorVisible
			decor.checkDecorVisible();

			
			
			// lens position
			if(perso.lens) perso.setLensPos();
			
		}
		
		public function get enable():Boolean { return _enable; }
		
		public function set enable(value:Boolean):void 
		{
			_enable = value;
		}
		
		public function get scrollEnable():Boolean { return _scrollEnable; }
		
		public function set scrollEnable(value:Boolean):void 
		{
			_scrollEnable = value;
		}
		
		

	}
}